<template>
  <div>
    <el-card>
      <el-tag>总记录数：{{ total }}</el-tag>
    </el-card>
    <el-card>
      <el-table
        :data="roleList"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="name"
          label="角位名称"
          width="300"
        />
        <el-table-column
          prop="description"
          label="角位介绍"
          style="width: 100%"
        />
        <el-table-column
          prop="id"
          label="角位编号"
          width="300"
        />
        <el-table-column

          label="操作"
          width="400"
        >
          <template #default="data">
            <el-button type="text" @click="addrole">添加</el-button>
            <el-button type="text" @click="editRole(data.row.id)">编辑</el-button>
            <el-button type="text" @click="delRole(data.row.id)">删除</el-button>
            <el-button type="text">分配权限</el-button>
          </template>
        </el-table-column>

      </el-table>
      <!-- 翻页 -->
      <div class="block">

        <el-pagination
          :current-page="settingsParams.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="settingsParams.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog
      title="提示"
      :visible="dialogVisible"
      width="30%"
      @close="closeDialog"
    >
      <template>
        <el-form ref="roleForm" label-width="120px" :model="roleInfo" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleInfo.name" style="width:80%" placeholder="1-12个字符" />
          </el-form-item>

          <el-form-item label="角色介绍" prop="description">
            <el-input v-model="roleInfo.description" style="width:80%" placeholder="1-300个字符" type="textarea" />
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click.prevent="submitRoleInfo">确 定</el-button>
        </span>
      </template>

    </el-dialog>
  </div>
</template>

<script>
import { fetchSettingsAPI, addRoleInfoAPI, editRoleFormAPI, updateRoleInfoAPI, delRoleInfoAPI } from '@/api/settings'
export default {
  data() {
    const checkName = (rule, value, callback) => {
      let arr = []
      if (this.roleInfo.id) {
        arr = this.roleName.filter(item => item !== this.roleId)
      } else {
        arr = this.roleName
      }
      if (arr.includes(value)) {
        callback(new Error('角色已被定义'))
      }
      callback()
    }
    return {
      roleName: [],
      roleId: '',
      roleInfo: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称必填', trigger: 'blur' },
          { min: 1, max: 12, message: '角色名称由1-12位字符组成', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色介绍必填', trigger: 'blur' },
          { min: 1, max: 300, message: '角色名称由1-300位字符组成', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      roleList: [],
      total: 0,
      settingsParams: {
        page: 1,
        pagesize: 10
      }
    }
  },
  mounted() {
    this.getRolelist()
  },
  methods: {
    clearFrom() {
      this.roleInfo = {
        name: '',
        description: '',
        id: ''
      }
    },
    // 获取页面
    async getRolelist() {
      const { rows, total } = await fetchSettingsAPI(this.settingsParams)
      this.roleList = rows
      this.total = total
      rows.forEach(item => {
        this.roleName.push(item.name)
      })
    },
    // 翻页
    handleSizeChange(pagesize) {
      this.settingsParams.pagesize = pagesize
      this.getRolelist()
    },
    handleCurrentChange(page) {
      this.settingsParams.page = page
      this.getRolelist()
    },
    // 关闭回调
    closeDialog() {
      this.$refs.roleForm.resetFields()
      this.clearFrom()
      this.dialogVisible = false
    },
    // 新增
    addrole() {
      this.dialogVisible = true
    },
    submitRoleInfo() {
      this.$refs.roleForm.validate(async(valid) => {
        if (!valid) return
        if (this.roleInfo.id) {
          await updateRoleInfoAPI(this.roleInfo)
        } else {
          await addRoleInfoAPI(this.roleInfo)
        }
        this.$message.success(`${this.roleInfo.id ? '编辑' : '添加'}成功`)
        this.closeDialog()
        this.getRolelist()
      })
    },
    // 编辑回填
    async editRole(id) {
      this.dialogVisible = true
      const res = await editRoleFormAPI(id)
      this.roleInfo = res
      this.roleId = res.name
    },
    delRole(id) {
      this.$confirm('确认删除该角色吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRoleInfoAPI(id)
        this.$message.success('删除成功')
        this.getRolelist()
      }).catch(() => {

      })
    }

  }
}

</script>

<style lang="css" scoped>
  .el-card{
    margin: 20px;
  }
</style>
